﻿using Autofac.Extensions.DependencyInjection;
using Autofac;
using Mes.Infrastructure.Data.BoundedContext.Repositories;
using Mes.Infrastructure.Data.BoundedContext.RepositoryBase;
using Mes.Application.Services.MockData;
using Mes.Infrastructure.Data.BoundedContext.Repositories.Operations;
using Mes.Application.Services.Operations;
using Mes.Infrastructure.Data.BoundedContext.Repositories.WorkOrders;
using Mes.Application.Services.WorkOrders;
using Mes.Application.Services.MasterData;
using Mes.Infrastructure.Data.BoundedContext.Repositories.MasterData;
using Mes.Infrastructure.Data.BoundedContext.Repositories.Quality;
using Mes.Application.Services.Quality;
using Autofac.Builder;

namespace Mes.Application.Web.AutoFac
{
    public static class IAutofacHostBuilderExtensions
    {
        public static IHostBuilder UseAutofac(this IHostBuilder hostBuilder)
        {
            return hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureContainer<ContainerBuilder>(containerBuilder =>
                {
                    containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
                    containerBuilder.RegisterGeneric(typeof(NoIdRepository<>)).As(typeof(INoIdRepository<>)).InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MockDataService>().InstancePerLifetimeScope();


                    #region MeasuringUnitType
                    containerBuilder.RegisterType<MeasuringUnitTypeRepository>().As<IMeasuringUnitTypeRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MeasuringUnitTypeService>().As<IMeasuringUnitTypeService>().InstancePerLifetimeScope();
                    #endregion

                    #region MaterialClassType
                    containerBuilder.RegisterType<MaterialClassTypeRepository>().As<IMaterialClassTypeRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialClassTypeService>().As<IMaterialClassTypeService>().InstancePerLifetimeScope();
                    #endregion

                    #region MeasuringUnit
                    containerBuilder.RegisterType<MeasuringUnitRepository>().As<IMeasuringUnitRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MeasuringUnitService>().As<IMeasuringUnitService>().InstancePerLifetimeScope();
                    #endregion

                    #region ProductionUnitType
                    containerBuilder.RegisterType<ProductionUnitTypeRepository>().As<IProductionUnitTypeRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ProductionUnitTypeService>().As<IProductionUnitTypeService>().InstancePerLifetimeScope();
                    #endregion

                    #region MaterialClass
                    containerBuilder.RegisterType<MaterialClassRepository>().As<IMaterialClassRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialClassService>().As<IMaterialClassService>().InstancePerLifetimeScope();
                    #endregion

                    #region Material
                    containerBuilder.RegisterType<MaterialRepository>().As<IMaterialRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialMaterialBOMLinkRepository>().As<IMaterialMaterialBOMLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialService>().As<IMaterialService>().InstancePerLifetimeScope();
                    #endregion

                    #region MaterialBOM
                    containerBuilder.RegisterType<MaterialBOMRepository>().As<IMaterialBOMRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialBOMMaterialLinkRepository>().As<IMaterialBOMMaterialLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<MaterialBOMService>().As<IMaterialBOMService>().InstancePerLifetimeScope();
                    #endregion

                    #region ProductionUnit
                    containerBuilder.RegisterType<ProductionUnitRepository>().As<IProductionUnitRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ProductionUnitService>().As<IProductionUnitService>().InstancePerLifetimeScope();
                    #endregion

                    #region Process
                    containerBuilder.RegisterType<ProcessRepository>().As<IProcessRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ProcessService>().As<IProcessService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ItemProcessLinkRepository>().As<IItemProcessLinkRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region StdOper
                    containerBuilder.RegisterType<StdOperRepository>().As<IStdOperRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperService>().As<IStdOperService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperEntLinkRepository>().As<IStdOperEntLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperAttrRepository>().As<IStdOperAttrRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperStepGrpRepository>().As<IStdOperStepGrpRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperStepRepository>().As<IStdOperStepRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region Attr
                    containerBuilder.RegisterType<AttrRepository>().As<IAttrRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<AttrService>().As<IAttrService>().InstancePerLifetimeScope();
                    #endregion

                    #region ProcessRoute
                    containerBuilder.RegisterType<ProcessRouteRepository>().As<IProcessRouteRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ProcessRouteService>().As<IProcessRouteService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<StdOperEntRouteRepository>().As<IStdOperEntRouteRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region Oper
                    containerBuilder.RegisterType<OperRepository>().As<IOperRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperService>().As<IOperService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperEntLinkRepository>().As<IOperEntLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperEntRouteRepository>().As<IOperEntRouteRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperStepGrpRepository>().As<IOperStepGrpRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperStepRepository>().As<IOperStepRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<OperAttrRepository>().As<IOperAttrRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<BomItemOperLinkRepository>().As<IBomItemOperLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<BomItemOperStepLinkRepository>().As<IBomItemOperStepLinkRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region JobState
                    containerBuilder.RegisterType<JobStateRepository>().As<IJobStateRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobStateService>().As<IJobStateService>().InstancePerLifetimeScope();
                    #endregion

                    #region Wo
                    containerBuilder.RegisterType<WoRepository>().As<IWoRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<WoService>().As<IWoService>().InstancePerLifetimeScope();
                    #endregion

                    #region Job
                    containerBuilder.RegisterType<JobBOMRepository>().As<IJobBOMRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobStepRepository>().As<IJobStepRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobStepGrpRepository>().As<IJobStepGrpRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobRouteRepository>().As<IJobRouteRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobRepository>().As<IJobRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobService>().As<IJobService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobBomStepRepository>().As<IJobBomStepRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region WoFlow
                    containerBuilder.RegisterType<ItemProdRepository>().As<IItemProdRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<WoFlowService>().As<IWoFlowService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobExecRepository>().As<IJobExecRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<JobStepDataRepository>().As<IJobStepDataRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<ItemConsRepository>().As<IItemConsRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region SampleFreq
                    containerBuilder.RegisterType<SampleFreqRepository>().As<ISampleFreqRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<SampleFreqService>().As<ISampleFreqService>().InstancePerLifetimeScope();
                    #endregion

                    #region SamplePlan
                    containerBuilder.RegisterType<SamplePlanRepository>().As<ISamplePlanRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<SamplePlanService>().As<ISamplePlanService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<SamplePlanFreqLinkRepository>().As<ISamplePlanFreqLinkRepository>().InstancePerLifetimeScope();
                    #endregion

                    #region Characteristic
                    containerBuilder.RegisterType<CharacteristicRepository>().As<ICharacteristicRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<CharacteristicService>().As<ICharacteristicService>().InstancePerLifetimeScope();
                    #endregion

                    #region SPCRule
                    containerBuilder.RegisterType<QmSpecRuleLinkRepository>().As<IQmSpecRuleLinkRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<SpcRuleRepository>().As<ISpcRuleRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<SpcRuleService>().As<ISpcRuleService>().InstancePerLifetimeScope();
                    #endregion

                    #region QmSpec
                    containerBuilder.RegisterType<QmSpecRepository>().As<IQmSpecRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<QmSpecService>().As<IQmSpecService>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<QmSpecCharLinkRepository>().As<IQmSpecCharLinkRepository>().InstancePerLifetimeScope();
                    #endregion
                });
        }
    }
}
